//
//  PhotoScanViewController.m
//  mafengwo
//
//  Created by Mac on 16/4/11.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "PhotoScanViewController.h"

@interface PhotoScanViewController ()<UIScrollViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>{

    UIScrollView *_scrollView;// 图片浏览
    NSInteger _currentImg;
}

@end

@implementation PhotoScanViewController

- (void)setDataList:(NSMutableArray *)dataList {
    _dataList = dataList;

}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    _currentImg = currentIndex;
    [_scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width *_currentIndex, -50, self.view.frame.size.width, self.view.frame.size.height) animated:NO];
}

-(id)init{
    self = [super init];
    if (self != nil) {
        self.view.backgroundColor = [UIColor blackColor];
        self.navigationItem.title = @"相册";
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"back_black.png"] forBarMetrics:UIBarMetricsDefault];

        //上面的方法都不行
//        [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"naviBar_background"] forBarMetrics:UIBarMetricsDefault];
//        [[UINavigationBar appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];


        //返回摁钮
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        [backButton setImage:[UIImage imageNamed:@"back_black.png"] forState:UIControlStateNormal];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = backItem;
        [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        //保存到本地
        UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(280, 0, 30, 30)];
        [saveButton setImage:[UIImage imageNamed:@"download_black.png"] forState:UIControlStateNormal];
        UIBarButtonItem *saveItem = [[UIBarButtonItem alloc]initWithCustomView:saveButton];
        self.navigationItem.rightBarButtonItem = saveItem;
        [saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];


    }
    return self;
}

-(void)backAction{


    //移除模态视图
    [self dismissViewControllerAnimated:YES completion:^{
        //        NSLog(@"移除时执行的代码");
        
    }];
}
//保存到相册
-(void)saveAction{
    NSString *urlString = _dataList[_currentImg];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
    UIImage *image = [UIImage imageWithData:data];
    if (image) {
        //图片保存
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }

}
//相册存储后调用的方法 固定方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"save failed:%@",error);
    }
    else
    {
        UIAlertController *AlertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];
        [AlertC addAction:alertAction];
        [self presentViewController:AlertC animated:YES completion:^{

        }];

    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    //数据源
    [self loadData];

    //底部滑动视图
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, -50, self.view.bounds.size.width, self.view.bounds.size.height)];
//    NSLog(@"%f",self.view.frame.size.height);
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width *_dataList.count, self.view.frame.size.height-64);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;

        //底部滑动视图上加滑动视图，再加imgView

    for (int i = 0; i<_dataList.count; i++) {
        // 实现放大浏览功能
        UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width *i, 0, self.view.frame.size.width, self.view.frame.size.height)];
        subScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

        subScrollView.maximumZoomScale = 4.0f;
        subScrollView.minimumZoomScale = 1.0f;
        subScrollView.delegate = self;
        [_scrollView addSubview:subScrollView];


        //每个小的滑动视图上加imgV
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:subScrollView.bounds];

        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        NSString *urlString = _dataList[i];
        imageView.tag = 101;
        [imageView setImageWithURL:[NSURL URLWithString:urlString]];
        [subScrollView addSubview:imageView];
    }

    // 根据_currentIndex设置滚动视图默认显示的区域


    [self.view addSubview:_scrollView];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat x=  scrollView.contentOffset.x;
    _currentImg = (int)x/KScreenW;
    NSLog(@"%ld",_currentImg);
}

-(void)loadData{
    NSString *pathStr = [[NSBundle mainBundle]pathForResource:@"qingdaodemo" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:pathStr];
    _dataList = [[NSMutableArray alloc]initWithCapacity:1];
    for (NSDictionary *dic in arr) {
        //放的是图片的URL
        [_dataList addObject:[dic objectForKey:@"img"]];
    }


}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    if (scrollView != _scrollView) {

        UIView *view = [scrollView viewWithTag:101];
        return view;
    }

//    NSLog(@"++++++");
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
